import pytest
from selenium import webdriver

# 打开浏览器，用例执行后唤起关闭浏览器的操作
driver = None
@pytest.fixture(scope="session", autouse=True)
def browser_enter():
    global driver
    if driver is None:
        driver = webdriver.Chrome()
        driver.maximize_window()
        yield driver
        driver.quit()

@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_makereport(item, call):
    pytest_html = item.config.pluginmanager.getplugin('html')
    outcom = yield
    report = outcom.get_result()
    extra = getattr(report, 'extra', [])
    if report.when == 'call':
        xfail = hasattr(report, 'wasxfail')
        if (report.skipped and xfail) or (report.failed and not xfail):
            screen = driver.get_screenshot_as_base64()
            extra.append(pytest_html.extras.png(screen))
            extra.append(pytest_html.extras.html('<div>Additional HTML</div>'))
        report.extra = extra